<?php
/**
 * Controller manager admin login, logout
 */
class MembersController extends AppController{
	
	var $name = 'Members';
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->CustomAuth->allow('admin_signin');
	}
	
	function admin_signin() {
		$role = $this->CustomAuth->user('role');
		if($role == 'admin'){
			$this->Session->write('Admin.isLogin', 1);
			$this->redirect(array($role => true, 'controller' => 'course', 'action' => 'index'));
		}
	}
	
	function admin_logout() {
		$this->Session->delete('Admin.isLogin');
		$this->CustomAuth->logout();
		$this->redirect(array('controller' => 'members', 'action' => 'signin'));
	}
	
	function admin_delete($userId) {
		$this->_checkIntParam($userId);
		$message = '';
		if(!empty($this->data)) {
			$userId = $this->data['User']['userId'];
			$this->loadModel('User');
			if($this->User->deleteAllDataOfUser($userId)) {
				$this->redirect(array('controller' => 'user_transactions', 'action' => 'index'));
			}else{
				$message = Messages::error_please_try_again;
			}
		}
		$this->loadModel('Profile');
		$this->loadModel('Course');
		$this->loadModel('Transaction');
		$profile = $this->Profile->getProFileByUserId($userId);
		$enteredNumber = $this->_getEnteredClassNumber($userId);
		$attendNumber = $this->_getAttendClassNumber($userId);
		$totalOpenClass = $this->Course->getNumberOfClassCreateByUser($userId);
		$successTransNumber = $this->Transaction->getNumberOfUserTransactionByStatus($userId, 1);
		$faultTransNumber = $this->Transaction->getNumberOfUserTransactionByStatus($userId, 0);
		$this->set(array(
			'userId' => $userId,
			'profile' => $profile,
			'enteredNumber' => $enteredNumber,
			'attendNumber' => $attendNumber,
			'totalOpenClass' => $totalOpenClass,
			'successTransNumber' => $successTransNumber,
			'faultTransNumber' => $faultTransNumber,
			'errorMessage' => $message
		));
	}
	
	function admin_addRecommendTeacher($userId) {
		$this->_checkIntParam($userId);
		$this->loadModel('RecommendTeacher');
		$result = $this->RecommendTeacher->add($userId);
		if($result == 1) {
			$this->Session->setFlash(Messages::successfully,'flash_message', array(), 'teachersuccess');
		}elseif($result == 0) {
			$this->Session->setFlash(Messages::recommend_teacher_is_exists,'flash_message', array(), 'teachererror');
		}else{
			$this->Session->setFlash(Messages::error_please_try_again,'flash_message', array(), 'teachererror');
		}
		$this->redirect($this->referer());
	}
	
	function admin_removeRecommendTeacher($id) {
		$this->_checkIntParam($id);
		$this->loadModel('RecommendTeacher');
		if(!$this->RecommendTeacher->isRecommendTeacherId($id)){
			$this->cakeError('error404');
		}
		if($this->RecommendTeacher->remove($id)){
			$this->Session->setFlash(Messages::successfully,'flash_message', array(), 'teachersuccess');
		}else{
			$this->Session->setFlash(Messages::error_please_try_again,'flash_message', array(), 'teachererror');
		}
		$this->redirect($this->referer());
	}
	function _checkIntParam($param) {
		if(!(isset($param))) {
			$this->cakeError('error404');
		}
		if(intval($param) < 1) {
			$this->cakeError('error404');
		}
	}

}
?>
